;KEYS.ASM


FIND_KEY:	CLR B
		LDS A,$402
		SBRS A,5
		LDI B,128
		CLR YL			;RESET KEYBOARD BUFFER

CHECK_LINE0:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE1
		LSL A
		BRCC LINE0_BIT7
		LSL A
		BRCC LINE0_BIT6
		LSL A
		BRCC LINE0_BIT5
		LSL A
		BRCC LINE0_BIT4
		LSL A
		BRCC LINE0_BIT3
		LSL A
		BRCC LINE0_BIT2
		LSL A
		BRCC LINE0_BIT1
		LSL A
		BRCC LINE0_BIT0

LINE0_BIT7:	LDI A,58		;F.
		RJMP KEY_END
LINE0_BIT6:	LDI A,66		;ENTER
		RJMP KEY_END
LINE0_BIT5:	LDI A,51		;FUNCTION 3
		RJMP KEY_END
LINE0_BIT4:	LDI A,54		;FUNCTION 6
		RJMP KEY_END
LINE0_BIT3:	LDI A,57		;FUNCTION 9
		RJMP KEY_END
LINE0_BIT2:	LDI A,62		;DOWN
		RJMP KEY_END
LINE0_BIT1:	LDI A,60		;RIGHT
		RJMP KEY_END
LINE0_BIT0:	LDI A,61		;UP
		RJMP KEY_END
		
CHECK_LINE1:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE2
		
		LSL A
		BRCC LINE1_BIT7
		LSL A
		BRCC LINE1_BIT6
		LSL A
		BRCC LINE1_BIT5
		LSL A
		BRCC LINE1_BIT4
		LSL A
		BRCC LINE1_BIT3
		LSL A
		BRCC LINE1_BIT2
		LSL A
		BRCC LINE1_BIT1
		LSL A
		BRCC LINE1_BIT0

LINE1_BIT7:	LDI A,48		;FUNCTION 0
		RJMP KEY_END
LINE1_BIT6:	LDI A,50		;FUNCTION 2
		RJMP KEY_END
LINE1_BIT5:	LDI A,49		;FUNCTION 1
		RJMP KEY_END
LINE1_BIT4:	LDI A,53		;FUNCTION 5
		RJMP KEY_END
LINE1_BIT3:	LDI A,56		;FUNCTION 8
		RJMP KEY_END
LINE1_BIT2:	LDI A,55		;FUNCTION 7
		RJMP KEY_END
LINE1_BIT1:	LDI A,68		;COPY
		RJMP KEY_END
LINE1_BIT0:	LDI A,59		;LEFT
		RJMP KEY_END

CHECK_LINE2:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE3
		LSL A
		BRCC LINE2_BIT7
		LSL A
		BRCC LINE2_BIT6
		LSL A
		LSL A
		BRCC LINE2_BIT4
		LSL A
		BRCC LINE2_BIT3
		LSL A
		BRCC LINE2_BIT2
		LSL A
		BRCC LINE2_BIT1
		LSL A
		BRCC LINE2_BIT0
		RJMP CHECK_LINE3
		
LINE2_BIT7:	LDI A,70		;CONTROL
		RJMP KEY_END
LINE2_BIT6:	LDI A,47		;BACKSLASH
		RJMP KEY_END
LINE2_BIT4:	LDI A,52		;FUNCTION 4
		RJMP KEY_END
LINE2_BIT3:	LDI A,43		;CLOSE BRACKET
		RJMP KEY_END
LINE2_BIT2:	LDI A,65		;RETURN
		RJMP KEY_END
LINE2_BIT1:	LDI A,40		;OPEN BRACKET
		RJMP KEY_END
LINE2_BIT0:	LDI A,63		;CLR
		RJMP KEY_END

CHECK_LINE3:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE4
		LSL A
		BRCC LINE3_BIT7
		LSL A
		BRCC LINE3_BIT6
		LSL A
		BRCC LINE3_BIT5
		LSL A
		BRCC LINE3_BIT4
		LSL A
		BRCC LINE3_BIT3
		LSL A
		BRCC LINE3_BIT2
		LSL A
		BRCC LINE3_BIT1
		LSL A
		BRCC LINE3_BIT0

LINE3_BIT7:	LDI A,45		;FULL STOP
		RJMP KEY_END
LINE3_BIT6:	LDI A,46		;SLASH
		RJMP KEY_END
LINE3_BIT5:	LDI A,41		;COLON
		RJMP KEY_END
LINE3_BIT4:	LDI A,42		;GR_QUESTION MARK
		RJMP KEY_END
LINE3_BIT3:	LDI A,16		;p
		RJMP KEY_END
LINE3_BIT2:	LDI A,39		;@
		RJMP KEY_END
LINE3_BIT1:	LDI A,37		;-
		RJMP KEY_END
LINE3_BIT0:	LDI A,38		;^
		RJMP KEY_END

CHECK_LINE4:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE5
		LSL A
		BRCC LINE4_BIT7
		LSL A
		BRCC LINE4_BIT6
		LSL A
		BRCC LINE4_BIT5
		LSL A
		BRCC LINE4_BIT4
		LSL A
		BRCC LINE4_BIT3
		LSL A
		BRCC LINE4_BIT2
		LSL A
		BRCC LINE4_BIT1
		LSL A
		BRCC LINE4_BIT0

LINE4_BIT7:	LDI A,44		;COMMA
		RJMP KEY_END
LINE4_BIT6:	LDI A,13		;m
		RJMP KEY_END
LINE4_BIT5:	LDI A,11		;k
		RJMP KEY_END
LINE4_BIT4:	LDI A,12		;l
		RJMP KEY_END
LINE4_BIT3:	LDI A,9			;i
		RJMP KEY_END
LINE4_BIT2:	LDI A,15		;o
		RJMP KEY_END
LINE4_BIT1:	LDI A,35		;9
		RJMP KEY_END
LINE4_BIT0:	LDI A,36		;0
		RJMP KEY_END

CHECK_LINE5:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE6
		LSL A
		BRCC LINE5_BIT7
		LSL A
		BRCC LINE5_BIT6
		LSL A
		BRCC LINE5_BIT5
		LSL A
		BRCC LINE5_BIT4
		LSL A
		BRCC LINE5_BIT3
		LSL A
		BRCC LINE5_BIT2
		LSL A
		BRCC LINE5_BIT1
		LSL A
		BRCC LINE5_BIT0

LINE5_BIT7:	LDI A,67		;SPACE
		RJMP KEY_END
LINE5_BIT6:	LDI A,14		;n
		RJMP KEY_END
LINE5_BIT5:	LDI A,10		;j
		RJMP KEY_END
LINE5_BIT4:	LDI A,8			;h
		RJMP KEY_END
LINE5_BIT3:	LDI A,25		;y
		RJMP KEY_END
LINE5_BIT2:	LDI A,21		;u
		RJMP KEY_END
LINE5_BIT1:	LDI A,33		;7
		RJMP KEY_END
LINE5_BIT0:	LDI A,34		;8
		RJMP KEY_END


CHECK_LINE6:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE7
		LSL A
		BRCC LINE6_BIT7
		LSL A
		BRCC LINE6_BIT6
		LSL A
		BRCC LINE6_BIT5
		LSL A
		BRCC LINE6_BIT4
		LSL A
		BRCC LINE6_BIT3
		LSL A
		BRCC LINE6_BIT2
		LSL A
		BRCC LINE6_BIT1
		LSL A
		BRCC LINE6_BIT0

LINE6_BIT7:	LDI A,22		;v
		RJMP KEY_END
LINE6_BIT6:	LDI A,2			;b
		RJMP KEY_END
LINE6_BIT5:	LDI A,6			;f
		RJMP KEY_END
LINE6_BIT4:	LDI A,7			;g
		RJMP KEY_END
LINE6_BIT3:	LDI A,20		;t
		RJMP KEY_END
LINE6_BIT2:	LDI A,18		;r
		RJMP KEY_END
LINE6_BIT1:	LDI A,31		;5
		RJMP KEY_END
LINE6_BIT0:	LDI A,32		;6
		RJMP KEY_END


CHECK_LINE7:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE8
		LSL A
		BRCC LINE7_BIT7
		LSL A
		BRCC LINE7_BIT6
		LSL A
		BRCC LINE7_BIT5
		LSL A
		BRCC LINE7_BIT4
		LSL A
		BRCC LINE7_BIT3
		LSL A
		BRCC LINE7_BIT2
		LSL A
		BRCC LINE7_BIT1
		LSL A
		BRCC LINE7_BIT0

LINE7_BIT7:	LDI A,24		;x
		RJMP KEY_END
LINE7_BIT6:	LDI A,3			;c
		RJMP KEY_END
LINE7_BIT5:	LDI A,4			;d
		RJMP KEY_END
LINE7_BIT4:	LDI A,19		;s
		RJMP KEY_END
LINE7_BIT3:	LDI A,23		;w
		RJMP KEY_END
LINE7_BIT2:	LDI A,5			;e
		RJMP KEY_END
LINE7_BIT1:	LDI A,29		;3
		RJMP KEY_END
LINE7_BIT0:	LDI A,30		;4
		RJMP KEY_END


CHECK_LINE8:	LD A,Y+
		CPI A,$FF
		BREQ CHECK_LINE9
		LSL A
		BRCC LINE8_BIT7
		LSL A
		BRCC LINE8_BIT6
		LSL A
		BRCC LINE8_BIT5
		LSL A
		BRCC LINE8_BIT4
		LSL A
		BRCC LINE8_BIT3
		LSL A
		BRCC LINE8_BIT2
		LSL A
		BRCC LINE8_BIT1
		LSL A
		BRCC LINE8_BIT0

LINE8_BIT7:	LDI A,26		;z
		RJMP KEY_END
LINE8_BIT6:	LDI A,69		;CAPS LOCK
		RJMP KEY_END
LINE8_BIT5:	LDI A,1			;a
		RJMP KEY_END
LINE8_BIT4:	LDI A,71		;TAB
		RJMP KEY_END
LINE8_BIT3:	LDI A,17		;q
		RJMP KEY_END
LINE8_BIT2:	LDI A,72		;ESCAPE
		RJMP KEY_END
LINE8_BIT1:	LDI A,28		;2
		RJMP KEY_END
LINE8_BIT0:	LDI A,27		;1
		RJMP KEY_END
		
CHECK_LINE9:	LD A,Y+
		LSL A
		BRCC LINE9_BIT7
		
NO_KEY_PRESSED:	CLR A

KEY_END:	OR A,B
		STS KEY_PRESSED,A
		STS KB_BUFFER,CLR_REG
		RJMP WR_END
		
LINE9_BIT7:	LDI A,64		;DELETE
		RJMP KEY_END
